package com.lg.okframe.study.designmode;

/**
 * 工厂模式
 * Created by LG on 2018/8/20.
 */

public class FactoryMode {


    /**
     * 简单工厂模式
     */

    static class SimpleFactory {

        public void use() {
            INoodles noodles = SimpleNoodlesFactory.createNoodles(SimpleNoodlesFactory.TYPE_LZ);
            noodles.desc();
        }

        //抽象方法
        public abstract static class INoodles {
            public abstract void desc();
        }

        //产品类
        public static class LzNoodles extends INoodles {
            @Override
            public void desc() {
                System.out.println("刀削面");
            }
        }

        //产品类
        public static class PaoNoodles extends INoodles {
            @Override
            public void desc() {
                System.out.println("泡面");
            }
        }

        //工厂类
        public static class SimpleNoodlesFactory {
            public static final int TYPE_LZ = 1;//刀削面
            public static final int TYPE_PM = 2;//泡面

            public static INoodles createNoodles(int type) {
                switch (type) {
                    case TYPE_LZ:
                        return new LzNoodles();
                    case TYPE_PM:
                        return new PaoNoodles();
                    default:
                        return new PaoNoodles();
                }
            }
        }
    }


    /**
     * 抽象工厂模式
     */

    static class AbstractFactory {
        //使用
        public void use() {
            IFactory factory = new Factory();
            factory.createProduct1().show();
            factory.createProduct2().show();
        }

        interface IProduct1 {
            public void show();
        }

        interface IProduct2 {
            public void show();
        }

        class Product1 implements IProduct1 {
            public void show() {
                System.out.println("这是1型产品");
            }
        }

        class Product2 implements IProduct2 {
            public void show() {
                System.out.println("这是2型产品");
            }
        }

        interface IFactory {
            public IProduct1 createProduct1();

            public IProduct2 createProduct2();
        }

        public class Factory implements IFactory {
            public IProduct1 createProduct1() {
                return new Product1();
            }

            public IProduct2 createProduct2() {
                return new Product2();
            }
        }

    }

}
